﻿//======================================================================
//
//        Copyright : Zhengzhou Strawberry Computer Technology Co.,LTD.
//        All rights reserved
//        
//        Application:NFinal MVC framework
//        Filename : StringContainer.cs
//        Description :字符串容器类，用于基本类型的自动转换。
//
//        created by Lucas at  2015-5-31
//     
//        WebSite:http://www.nfinal.com
//
//======================================================================
using System;

namespace NFinal
{
	/// <summary>
    /// 字符串容器类，用于基本类型的自动转换。
    /// </summary>
    public struct BytesContainer
    {
		/// <summary>
		/// 空容器类，用于判断初始化等。
		/// </summary>
        public static readonly byte[] Empty=new BytesContainer();
		/// <summary>
		/// bytes数组
		/// </summary>
        public byte[] value;
		/// <summary>
		/// bytes数组数据起始位置
		/// </summary>
		public int index;
		/// <summary>
		/// bytes数组数据的长度
		/// </summary>
		public int length;
		/// <summary>
        /// 初始化函数
        /// </summary>
        /// <param name="value"></param>
        internal BytesContainer(byte[] value)
        {
            this.value = value;
			this.index=0;
			this.length=0;
        }
		/// <summary>
        /// 比较判断
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
		public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        /// <summary>
        /// GetHashCode
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        /// <summary>
        /// 相等判断
        /// </summary>
        /// <param name="container"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
		public static bool operator ==(BytesContainer container, byte[] bytes)
        {
            return container.value == bytes;
        }
        /// <summary>
        /// 不等判断
        /// </summary>
        /// <param name="container"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static bool operator !=(BytesContainer container, byte[] bytes)
        {
            return container.value != bytes;
        }
		/// <summary>
        /// 初始化函数
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
		public static implicit operator BytesContainer(byte[] bytes)
		{
			return new BytesContainer(bytes);
		}
		/// <summary>
        /// 把SByte类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">SByte类型</param>
        public static implicit operator BytesContainer(SByte obj)
		{
			return new BytesContainer(new byte[] { (byte)obj });
		}
		/// <summary>
        /// 把Byte类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">Byte类型</param>
        public static implicit operator BytesContainer(Byte obj)
		{
			return new BytesContainer(new byte[] { obj });
		}
		/// <summary>
        /// 把Int16类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">Int16类型</param>
        public static implicit operator BytesContainer(Int16 obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把UInt16类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">UInt16类型</param>
        public static implicit operator BytesContainer(UInt16 obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把Int32类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">Int32类型</param>
        public static implicit operator BytesContainer(Int32 obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把UInt32类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">UInt32类型</param>
        public static implicit operator BytesContainer(UInt32 obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把Int64类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">Int64类型</param>
        public static implicit operator BytesContainer(Int64 obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把UInt64类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">UInt64类型</param>
        public static implicit operator BytesContainer(UInt64 obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把Boolean类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">Boolean类型</param>
        public static implicit operator BytesContainer(Boolean obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把Char类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">Char类型</param>
        public static implicit operator BytesContainer(Char obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把Decimal类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">Decimal类型</param>
        public static implicit operator BytesContainer(Decimal obj)
		{
			return new BytesContainer(BitConverterExt.GetBytes(obj));
		}
		/// <summary>
        /// 把Double类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">Double类型</param>
        public static implicit operator BytesContainer(Double obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把Single类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">Single类型</param>
        public static implicit operator BytesContainer(Single obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj));
		}
		/// <summary>
        /// 把DateTime类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">DateTime类型</param>
        public static implicit operator BytesContainer(DateTime obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj.Ticks));
		}
		/// <summary>
        /// 把DateTimeOffset类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">DateTimeOffset类型</param>
        public static implicit operator BytesContainer(DateTimeOffset obj)
		{
			return new BytesContainer(BitConverter.GetBytes(obj.Ticks));
		}
		/// <summary>
        /// 把SByte类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的SByte类型</param>
        public static implicit operator BytesContainer(SByte? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把Byte类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的Byte类型</param>
        public static implicit operator BytesContainer(Byte? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把Int16类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的Int16类型</param>
        public static implicit operator BytesContainer(Int16? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把UInt16类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的UInt16类型</param>
        public static implicit operator BytesContainer(UInt16? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把Int32类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的Int32类型</param>
        public static implicit operator BytesContainer(Int32? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把UInt32类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的UInt32类型</param>
        public static implicit operator BytesContainer(UInt32? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把Int64类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的Int64类型</param>
        public static implicit operator BytesContainer(Int64? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把UInt64类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的UInt64类型</param>
        public static implicit operator BytesContainer(UInt64? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把Boolean类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的Boolean类型</param>
        public static implicit operator BytesContainer(Boolean? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把Char类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的Char类型</param>
        public static implicit operator BytesContainer(Char? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把Decimal类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的Decimal类型</param>
        public static implicit operator BytesContainer(Decimal? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverterExt.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把Double类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的Double类型</param>
        public static implicit operator BytesContainer(Double? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把Single类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的Single类型</param>
        public static implicit operator BytesContainer(Single? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把DateTime类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的DateTime类型</param>
        public static implicit operator BytesContainer(DateTime? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value.Ticks));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 把DateTimeOffset类型转为字节容器类，即字节数组
        /// </summary>
        /// <param name="obj">可以为null的DateTimeOffset类型</param>
        public static implicit operator BytesContainer(DateTimeOffset? obj)
		{
            if(obj!=null)
            {
				return new BytesContainer(BitConverter.GetBytes(obj.Value.Ticks));
            }
			return BytesContainer.Empty;
		}
		/// <summary>
        /// 初始化函数
        /// </summary>
        /// <param name="bytesContainer">字节容器类</param>
		public static implicit operator byte[](BytesContainer bytesContainer)
		{
			return bytesContainer.value;
		}
		/// <summary>
        /// 初始化函数
        /// </summary>
        /// <param name="value">字符串</param>
		public static implicit operator BytesContainer(string value)
        {
            return new BytesContainer(System.Text.Encoding.UTF8.GetBytes(value));
        }
		/// <summary>
        /// 初始化函数
        /// </summary>
        /// <param name="bytesContainer">字节容器类</param>
		public static implicit operator string(BytesContainer bytesContainer)
		{
			if (bytesContainer.index == 0 && bytesContainer.length==0)
            {
                return System.Text.Encoding.UTF8.GetString(bytesContainer.value);
            }
            else
            {
                return System.Text.Encoding.UTF8.GetString(bytesContainer.value,bytesContainer.index,bytesContainer.length);
            }
		}
		/// <summary>
        /// 把字节容器类转换为SByte类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator SByte(BytesContainer bytesContainer)
        {
			return (SByte)bytesContainer.value[0];
        }
		/// <summary>
        /// 把字节容器类转换为Byte类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Byte(BytesContainer bytesContainer)
        {
			return bytesContainer.value[0];
        }
		/// <summary>
        /// 把字节容器类转换为Int16类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Int16(BytesContainer bytesContainer)
        {
			return BitConverter.ToInt16(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为UInt16类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator UInt16(BytesContainer bytesContainer)
        {
			return BitConverter.ToUInt16(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为Int32类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Int32(BytesContainer bytesContainer)
        {
			return BitConverter.ToInt32(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为UInt32类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator UInt32(BytesContainer bytesContainer)
        {
			return BitConverter.ToUInt32(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为Int64类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Int64(BytesContainer bytesContainer)
        {
			return BitConverter.ToInt64(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为UInt64类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator UInt64(BytesContainer bytesContainer)
        {
			return BitConverter.ToUInt64(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为Boolean类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Boolean(BytesContainer bytesContainer)
        {
			return BitConverter.ToBoolean(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为Char类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Char(BytesContainer bytesContainer)
        {
			return BitConverter.ToChar(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为Decimal类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Decimal(BytesContainer bytesContainer)
        {
			return BitConverterExt.ToDecimal(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为Double类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Double(BytesContainer bytesContainer)
        {
			return BitConverter.ToDouble(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为Single类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Single(BytesContainer bytesContainer)
        {
			return BitConverter.ToSingle(bytesContainer.value,bytesContainer.index);
        }
		/// <summary>
        /// 把字节容器类转换为DateTime类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator DateTime(BytesContainer bytesContainer)
        {
			return new DateTime(BitConverter.ToInt64(bytesContainer.value,bytesContainer.index));
        }
		/// <summary>
        /// 把字节容器类转换为DateTimeOffset类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator DateTimeOffset(BytesContainer bytesContainer)
        {
			return new DateTimeOffset(BitConverter.ToInt64(bytesContainer.value,bytesContainer.index),TimeSpan.Zero);
        }
		/// <summary>
        /// 把字节容器类转换为可为null的SByte类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator SByte?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return (SByte)bytesContainer.value[0];
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的Byte类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Byte?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return bytesContainer.value[0];
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的Int16类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Int16?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToInt16(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的UInt16类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator UInt16?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToUInt16(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的Int32类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Int32?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToInt32(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的UInt32类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator UInt32?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToUInt32(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的Int64类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Int64?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToInt64(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的UInt64类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator UInt64?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToUInt64(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的Boolean类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Boolean?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToBoolean(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的Char类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Char?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToChar(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的Decimal类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Decimal?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverterExt.ToDecimal(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的Double类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Double?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToDouble(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的Single类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator Single?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return BitConverter.ToSingle(bytesContainer.value,bytesContainer.index);
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的DateTime类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator DateTime?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return new DateTime(BitConverter.ToInt64(bytesContainer.value,bytesContainer.index));
			}
			else
			{
				return null;
			}
        }
		/// <summary>
        /// 把字节容器类转换为可为null的DateTimeOffset类型
        /// </summary>
        /// <param name="bytesContainer">字节容器</param>
        public static implicit operator DateTimeOffset?(BytesContainer bytesContainer)
        {
            if(bytesContainer.value!=null)
			{
				return new DateTimeOffset(BitConverter.ToInt64(bytesContainer.value,bytesContainer.index),TimeSpan.Zero);
			}
			else
			{
				return null;
			}
        }
    }
}
